package com.sms.user;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.util.*;

public class GetTeacherServlet extends HttpServlet {
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		
		List<Teacher> tlist = null;
		String proID = request.getParameter("proID");
		if(proID == null || proID.equals("")) {
			tlist = User.getTeacher();
		} else {
			int proid = Integer.parseInt(proID);
			tlist = User.getTeacher(proid);
		}
		
		JSONArray json = new JSONArray();
		if (tlist != null && tlist.size() > 0) {
				for (int i = 0; i < tlist.size(); i++) {
					Teacher t = (Teacher) tlist.get(i);
					String tname = t.getTname();
					JSONObject j = new JSONObject();
					j.put("tno", t.getTno());
					j.put("tname", tname);
					j.put("sex", t.getSex());
					j.put("number", t.getItemCount());
					List<String> s = t.getStudents();
					String student = "";
					if(s != null && s.size()>0) {
						for(int m=0; m<s.size(); m++) {
							student += s.get(m) + ",";
						}
					} else {
						student = "无";
					}
					j.put("student", student);
					json.add(j);
					
				}
		} else {
			System.out.println("暂无老师信息");
		}
		out.println(json);
		
		out.flush();
		out.close();
	}

	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}
	
	

}
